package util.components;




import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;

import util.filters.CharacterLengthDocumentFilter;

@SuppressWarnings("serial")
public class JLimitedTextField extends JTextField{
	
	int totalChars;
	boolean canHaveLessChars;

	public JLimitedTextField(int duzina, int totalChars, boolean canHaveLess){
		super(duzina);
		this.totalChars=totalChars;
		this.canHaveLessChars=canHaveLess;
		AbstractDocument doc = (AbstractDocument)getDocument();
		CharacterLengthDocumentFilter filter = new CharacterLengthDocumentFilter();
		filter.setMaxLength(totalChars);
		doc.setDocumentFilter(filter);
	}
	

	public boolean isEditValid(){
		if (canHaveLessChars)
			return getText().length()>0;
		return getText().length()==totalChars;
	}
}
